﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.Entities_DAL
{
    public class Chungcu_DAL: BaseDAL, UTL.IBaseDAL
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            var sql = @"SELECT * FROM CHUNG_CU";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            string sql = @"DELETE FROM CHUNG_CU WHERE Ma_CC = '{0}'";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Chungcu)obj;
            string sql = @"INSERT INTO CHUNG_CU(Ma_CC, TEN_CC, DIA_CHI) VALUES('{0}',N'{1}', N'{2}') ";
            sql = string.Format(sql, o.MA_CC, o.TEN_CC, o.DIA_CHI);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Chungcu)obj;
            string sql = @"SET dateformat dmy; UPDATE CHUNG_CU SET TEN_CC=N'{1}', DIA_CHI=N'{2}' WHERE Ma_CC='{0}'";
            sql = string.Format(sql, o.MA_CC, o.TEN_CC, o.DIA_CHI);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }

        //tim ma so max
        public DataTable Select_max()
        {
            var sql = @"SELECT MAX(Ma_CC) AS id FROM CHUNG_CU";
            return ExecuteQuery(sql);
        }
        //dem so chung cu

        public string Select_count()
        {
            string sql = @"SELECT COUNT(Ma_CC) AS id FROM CHUNG_CU";
            var tb = ExecuteQuery(sql);
            return tb.Rows.Count > 0 ? tb.Rows[0]["id"] + "" : "0";
        }
    }
}
